<?php 
session_start();
require_once ("class/m_customer.php");

	$m = new M_Customer();
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				if ($id==0){
					$id=$_SESSION['custid'];
				}
				//$id=1;
				echo json_encode( $m->getDataDetail($id) );
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']=='runningCode'){
			$no=$m->getLastRunning($_SESSION['branchcode']);
			$code=$_SESSION['branchcode'].'-'.str_repeat('0', 6 - strlen($no) ) .$no;
			echo $code;
			
		}else if ($_POST['m']== 'card'){
			global $posScreen;

			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				$result = $m->getCustomerSmartPurse($_SESSION['custid']) ;
				//print_r($result);
				$rows=$result->RecordCount();
				if ($rows>0){
					$html="";
					$k=1;
					while($row = $result->FetchRow()){
						$html .="<tr>"
							."<td align='right'>".$k."</td>"
							."<td align='right' style='display:none;'>".$row["ID"]."</td>"
							."<td align='center'>".$row["HistoryDate"]."</td>"
							."<td align='center'>".$row["CardNo"]."</td>"
							."<td align='right'>".$row["Amount"]."</td>";
							
						if ($_SESSION["permission"][$posScreen]["Add"]=="1") { 	
							$html.="<td align='center'><img src='images/remove16.png' style='cursor:pointer' id='btnRemove' border=0/></td>";
						}else{
							$html.="<td align='center'> </td>";
						}
						$html."</tr>";
						$k++;
					}
					echo $html;
				}
				else{
					echo "<tr><td colspan=6/></tr>";
				}
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']=='delcard'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delSmartPurse($o["id"]);
			}
		}else if ($_POST['m']=='addcard'){
			$ar= $_POST['var'];
			$ar["BranchID"] = $_SESSION['bid'];
			$ar["CreatedBy"]=$m->getUserLogin();
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			$id=$m->InsertSmartPurse($ar);
			
			$result = $m->getCustomerSmartPurse( $_SESSION['custid']) ;
			//print_r($result);
			$rows=$result->RecordCount();
			if ($rows>0){
				$html="";
				$k=1;
				while($row = $result->FetchRow()){
					$html .="<tr>"
						."<td align='right'>".$k."</td>"
						."<td align='right' style='display:none;'>".$row["ID"]."</td>"
						."<td align='center'>".$row["HistoryDate"]."</td>"
						."<td align='center'>".$row["CardNo"]."</td>"
						."<td align='right'>".$row["Amount"]."</td>"
						."<td align='center'><img src='images/remove16.png' style='cursor:pointer' id='btnRemove' border=0/></td>"
					."</tr>";
					$k++;
				}
				echo $html;
			}
		}else if ($_POST['m']== 'add'){
		
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$_SESSION['username'];
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			$Bday="";
			if ($ar["BirthDay"]!=""){
				$Bday=$ar["BirthDay"];
				list($day, $month, $year) = split('/', $ar["BirthDay"]);
				$ar["BirthDay"]=$year."/".$month."/".$day;
			}
			if ($m->checkCustomerCode($ar["Code"])=="0"){
				echo "error";
			}else{
				$id=$m->InsertData($ar);
				//print_r($id);
				echo "<tr>".genTable($m->getDataDetail($id),$_POST['rows']+1)."</tr>";
			}
		
		}else if ($_POST['m']== 'main_addcust'){
		
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$_SESSION['username'];
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			$Bday="";
			
			if ($ar["BirthDay"]!=""){
				$Bday=$ar["BirthDay"];
				list($day, $month, $year) = split('/', $ar["BirthDay"]);
				$ar["BirthDay"]=$year."/".$month."/".$day;
			}
			if ($m->checkCustomerCode($ar["Code"])=="0"){
				$_SESSION['custid']="0";
				echo "error";
			}else{
				$id=$m->InsertData($ar);
				$_SESSION['custid']=$id;
				//print_r($id);
				echo  json_encode($m->getDataDetail($id));
			}
				
		}else if ($_POST['m']=='edit'){
		
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				
				list($day, $month, $year) =  split('/',$ar["CreatedDate"]);
				$ar["CreatedDate"]=$year."/".$month."/".$day;
				
				$ar["UpdatedBy"]=$_SESSION['username'];
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$Bday="";
				if ($ar["BirthDay"]!=""){
					$Bday=$ar["BirthDay"];
					list($day, $month, $year) = split('/', $ar["BirthDay"]);
					$ar["BirthDay"]=$year."/".$month."/".$day;
				}				
				
				$sqlScript="select * from tb_customer where IsDeleted=0 and ID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);	
			
				echo genTable($m->getDataDetail($_POST['id']),$_POST['rows']);
			}
					
		}else if ($_POST['m']=='main_editcust'){
		
			if (isset($_POST['var'])){
				$ar=$_POST['var'];

				$ar["UpdatedBy"]=$_SESSION['username'];
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$Bday="";
				if ($ar["BirthDay"]!=""){
					$Bday=$ar["BirthDay"];
					list($day, $month, $year) = split('/', $ar["BirthDay"]);
					$ar["BirthDay"]=$year."/".$month."/".$day;
				}				
				
				$sqlScript="select * from tb_customer where IsDeleted=0 and ID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);	
			
				echo json_encode($m->getDataDetail($_POST['id']));
			}
		
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"]);
			}
		}else if ($_POST['m']== 'search'){
			$name=$_POST['name'];
			$code=$_POST['code'];
			$bfrom=$_POST['birthdayfrom'];
			$bto=$_POST['birthdayto'];
			$lastfrom=$_POST['lastfrom'];
			$lastto=$_POST['lastto'];
			$page=$_POST['page'];
			$cardid=$_POST['cardid'];

			$page=isset($page)?$page:1;	
			$result=$m->getData($name,$code,$bfrom,$bto,$lastfrom,$lastto,$page ,$cardid);
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows > 0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					$html .= genTable($row,$i);
					$html .= "</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan='12' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
 
		}else if ($_POST['m']== 'search2'){
			$name=$_POST['name'];
			$lastname=$_POST['lastname'];
			$code=$_POST['code'];
			$bfrom=$_POST['birthdayfrom'];
			$bto=$_POST['birthdayto'];
			$lastfrom=$_POST['lastfrom'];
			$lastto=$_POST['lastto'];
			$page=$_POST['page'];
			$cardid=$_POST['cardid'];

			$page=isset($page)?$page:1;	
			$result=$m->getData($name,$code,$bfrom,$bto,$lastfrom,$lastto,$page ,$cardid,$lastname);
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows > 0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					$html .= genTable2($row,$i);
					$html .= "</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan='9' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
 
		}
	}else{echo"Error2";}

	function genTable2($row,$i){
		global $customerScreen;
		$s.="<td align='center'>".$i."</td>" .
				"<td style='display:none;'>".$row["ID"]."</td>" .
				"<td align='center'>".$row["Code"]."</td>" .
				"<td align='left'>".$row["FirstName"]." ".$row["LastName"]."</td>" .
				"<td align='center'>".$row["BirthDay"]."</td>" .
				"<td align='center'>".$row["CardID"]."</td>" .
				"<td align='center'>".$row["Telephone"]."</td>" .
				"<td align='center'>".$row["LastedAccess"]."</td>" .
				"<td align='center'>".$row["UpdatedDate"]."</td>" ;
				
	
		$s.="<td align='center'><img src='./images/view1.png' class='btnHistory' style='cursor:pointer' title='Customer History' border='0'/></td>";
 
		return $s;		
		
	}
	function genTable($row,$i){
		global $customerScreen;
		$s.="<td align='center'>".$i."</td>" .
				"<td style='display:none;'>".$row["ID"]."</td>" .
				"<td align='center'>".$row["Code"]."</td>" .
				"<td align='left'>".$row["FirstName"]." ".$row["LastName"]."</td>" .
				"<td align='left'>".$row["NickName"]."</td>" .
				"<td align='center'>".$row["BirthDay"]."</td>" .
				"<td align='center'>".$row["Telephone"]."</td>" .
				"<td align='center'>".$row["LastedAccess"]."</td>" .
				"<td align='center'>".$row["CreatedDate"]."</td>" ;
				
	 
					
					$s.="<td align='center'><img src='./images/history.png' class='btnHistory' style='cursor:pointer' title='Customer History' border='0'/></td>";

					
				if ($_SESSION["permission"][$customerScreen]["Edit"]=="1")
					$s.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
				else
					$s.="<td>&nbsp;</td>";
					
				if ($_SESSION["permission"][$customerScreen]["Deleted"]=="1")
					$s.="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
				else
					$s.="<td>&nbsp;</td>";
					
		return $s;
	}

?>